package com;

import com.ycy.dal.mapper.mbg.UserMapper;
import com.ycy.dal.model.mbg.User;
import com.ycy.dal.model.mbg.UserExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;

public class MybatisDemo {

  static SqlSessionFactory sqlSessionFactory;

  @Before
  public void before() {
    String resource = "mybatis-config.xml";
    InputStream inputStream = null;
    try {
      inputStream = Resources.getResourceAsStream(resource);
    } catch (IOException e) {
      e.printStackTrace();
    }
    //SqlSessionFactoryBuilder 这个实例用完就可以抛弃了
    sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
  }


  /*
  CREATE TABLE `user` (
    `user_id` int(11) AUTO_INCREMENT COMMENT 'id',
    `username` varchar(50) DEFAULT NULL,
    `addtime` timestamp,
    PRIMARY KEY (`user_id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
   */
  @Test
  public void test() {
    SqlSession sqlSession = null;
    try {
      sqlSession = sqlSessionFactory.openSession();
      User user = sqlSession.selectOne("com.ycy.dal.mapper.mbg.UserMapper.selectByPrimaryKey", 1);
      System.out.println(user);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      sqlSession.close();
    }
  }

  @Test
  public void test1() {
    SqlSession sqlSession = null;
    try {
      sqlSession = sqlSessionFactory.openSession();
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);
      User user = mapper.selectByPrimaryKey(11);
      System.out.println(user);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      sqlSession.close();
    }
  }

  /**
   * 一个sqlsession可以回滚、提交事务后，继续使用
   */
  @Test
  public void test11() {
    SqlSession sqlSession = null;
    try {

      //自己设置的 commit，自己要手动提交
      sqlSession = sqlSessionFactory.openSession(false);
      UserMapper mapper = sqlSession.getMapper(UserMapper.class);

      UserExample example = new UserExample();
      ArrayList<String> values = new ArrayList<>();
      values.add("王二1");
      values.add("王二2");
      values.add("王二3");
      UserExample.Criteria criteria = example.createCriteria().andUsernameIn(values);
      int i1 = mapper.deleteByExample(example);
      sqlSession.commit();

      // 回滚
      User record = new User();
      record.setUsername("王二1");
      int i = mapper.insert(record);
      sqlSession.rollback();

      // 提交
      User record2 = new User();
      record2.setUsername("王二2");
      mapper.insert(record2);
      sqlSession.commit();

      // 回滚
      User record3 = new User();
      record3.setUsername("王二3");
      mapper.insert(record3);
      sqlSession.rollback();

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      sqlSession.close();
    }
  }


}
